package com.demoandroid.demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.demoandroid.demo.file_manage.FileEx;
import com.demoandroid.demo.widget.Widget;
import com.demoandroid.demo.x5.X5TbsFileServicePage;

import com.demoandroid.demo.x5.FileModel;
import com.demoandroid.demo.x5.X5TbsWebViewPage;
import com.tencent.smtt.sdk.TbsVideo;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * 主页按钮 点击事件
     * */
    public void onClick(View view){

        Intent intent = null;

        switch (view.getId()){

            case R.id.file_btn:
                intent = new Intent();
                intent.setClass(MainActivity.this, FileEx.class);
                break;

            case R.id.x5_btn:

                //检查权限（NEED_PERMISSION）是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {
                    //用户已经拒绝过一次，再次弹出权限申请对话框需要给用户一个解释
                    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission
                            .WRITE_EXTERNAL_STORAGE)) {
                        Toast.makeText(this, "请开通相关权限，否则无法正常使用本应用！", Toast.LENGTH_SHORT).show();
                    }
                    //申请权限
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

                } else {
                    intent = new Intent();
                    intent.setClass(MainActivity.this, X5TbsFileServicePage.class);
                }
                break;

            case R.id.x5web_btn:
                FileModel fileModel = new FileModel();
                fileModel.setPath("/storage/emulated/0/demo/pdf.pdf");
                intent = new Intent();
                intent.setClass(MainActivity.this, X5TbsWebViewPage.class);
                break;
            case R.id.x5Video_btn:
                startPlay("/storage/emulated/0/demo/mp4.mp4");
                break;
            case R.id.widget_btn:
                intent = new Intent();
                intent.setClass(MainActivity.this, Widget.class);
                break;

        }
        //启动
        if (intent!=null){
            startActivity(intent);
        };
    }

    /**
     * 直接调用播放视频
     * @param videoUrl 视频地址
     */
    private void startPlay(String videoUrl){
        //判断当前是否可用
        if(TbsVideo.canUseTbsPlayer(getApplicationContext())){
            //播放视频
            TbsVideo.openVideo(getApplicationContext(), videoUrl);
        }
    }

}